//
//  ViewController.m
//  WhereAml
//
//  Created by Justsoso on 16/4/1.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

#import <MapKit/MapKit.h>

#import "Place.h"

@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *latitydeLabel;

@property (strong,nonatomic) CLLocationManager *locationManager;

@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)btnPressed:(id)sender
{
    self.locationManager = [[CLLocationManager alloc] init];
    
    // 精度
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // 代理人
    self.locationManager.delegate = self;
    // 更新距离
    self.locationManager.distanceFilter = 10.0f;
    // 请求用户权限
    [self.locationManager requestWhenInUseAuthorization];
    // 启动定位
    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    self.longitudeLabel.text = [NSString stringWithFormat:@"%.5f",newLocation.coordinate.longitude];
    self.latitydeLabel.text = [NSString stringWithFormat:@"%.5f",newLocation.coordinate.latitude];
    
    // 关闭更新
    [self.locationManager stopUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    // 1. 将得到的经纬度显示到界面
    CLLocation *newLocation = [locations lastObject];
    self.longitudeLabel.text = [NSString stringWithFormat:@"%.5f",newLocation.coordinate.longitude];
    self.latitydeLabel.text = [NSString stringWithFormat:@"%.5f",newLocation.coordinate.latitude];
    
    // 2. 设定地图
    // 范围大小
    MKCoordinateSpan span = {0.01,0.01};
    // 区域
    MKCoordinateRegion region = {newLocation.coordinate,span};
    [self.mapView setRegion:region];
    
    // 3. 根据经纬度信息 -> 位置信息 （从网络上获取）
    CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
    [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        CLPlacemark *place = [placemarks lastObject];
        NSLog(@"位置名称:%@",place.name);
        NSLog(@"位置街道:%@",place.thoroughfare);
        NSLog(@"位置子街道:%@",place.subThoroughfare);
        NSLog(@"位置区:%@",place.subLocality);
        NSLog(@"位置城市:%@",place.locality);
        NSLog(@"位置国家:%@",place.country);
    }];
    
    // 4. 添加标注
    Place *myPlace = [[Place alloc] init];
    myPlace.coordinate = newLocation.coordinate;
    myPlace.title = @"我的工作地点";
    myPlace.subtitle = @"震泽路18号";
    self.mapView.delegate = self;
    [self.mapView addAnnotation:myPlace];
    
    
    // 关闭更新
//    [self.locationManager stopUpdatingLocation];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKPinAnnotationView *pinView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"Anntotaion"];
    if (pinView == nil)
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Anntotaion"];
    }
    
    pinView.pinTintColor = [UIColor orangeColor];
    pinView.animatesDrop = YES;
    pinView.canShowCallout = YES;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    btn.tag = 101;
    [btn addTarget:self action:@selector(pinBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = btn;
    
    return pinView;
}

- (void)pinBtnPressed:(UIButton *)btn
{
    [self performSegueWithIdentifier:@"gotoVC" sender:nil];
}

@end
